package work2;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class YuangongDaolmpl extends BaseDao implements IYuangong {
    @Override
    public void add(Yuangong yuangong) {
        this.setConnection();
        try {
            ps=con.prepareStatement(
                    "insert into t_yuangong(y_name,y_zhiwei,y_birthday,y_gongzi)values(?,?,?,?) ");
            ps.setObject(1,yuangong.getName());
            ps.setObject(2, yuangong.getZhiwei());
            ps.setObject(3, yuangong.getBirthday().toString());
            ps.setObject(4, yuangong.getGongzi());

            //更新数据库
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }

    }

    @Override
    public void del(String name) {
        this.setConnection();
        try {
            ps=con.prepareStatement("delete  from t_yuangong where y_name=?");
            ps.setObject(1,name);
            ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }

    }

    @Override
    public void update(String name, String zhiwei, int gongzi) {
        this.setConnection();
        try {
            ps=con.prepareStatement("update t_yuangong set y_zhiwei=? , y_gongzi=?  where y_name=?");
            ps.setObject(1,zhiwei);
            ps.setObject(2,gongzi);
            ps.setObject(3,name);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }

    }
    private Yuangong getYg()throws SQLException{
        Yuangong yg=new Yuangong();
        yg.setId(rs.getInt("pk_yuangong"));
        yg.setName(rs.getString("y_name"));
        yg.setZhiwei(rs.getString("y_zhiwei"));
        yg.setBirthday(LocalDate.parse(rs.getString("y_birthday")));
        yg.setGongzi(rs.getInt("y_gongzi"));
        return yg;

    }

    @Override
    public List<Yuangong> findAll() {
        List<Yuangong> list=new ArrayList<>();
        this.setConnection();
        try {
            ps=con.prepareStatement("select *from t_yuangong");
            rs=ps.executeQuery();
            while (rs.next()) {

                list.add(this.getYg());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }

        return list;
    }

    @Override
    public Yuangong findByname(String name) {
        Yuangong b=null;
        this.setConnection();
        try {
            ps=con.prepareStatement("select *from t_yuangong where y_name=? ");
            ps.setObject(1,name);


            rs=ps.executeQuery();

            if (rs.next()){
                b= this.getYg();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }

        return b;
    }

    public static void main(String[] args) {
        IYuangong dao=new YuangongDaolmpl();
//                dao.add(new Yuangong("沐雪","销售员",LocalDate.parse("2000-11-05"),9999));
//        dao.del("沐雪");
//        dao.update("沐雪","经理",13578);
       System.out.println(dao.findAll());
//                System.out.println(dao.findByname("沐雪"));
    }
}
